package com.neudu.yyzx_service.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neudu.yyzx_service.common.JSONReturn;
import com.neudu.yyzx_service.pojo.Customer;
import com.neudu.yyzx_service.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    @Autowired
    private JSONReturn jsonReturn;

    @GetMapping("/getCustomers")
    public ResponseEntity<List<Customer>> getCustomers() {
        List<Customer> customers = customerService.list();
        return ResponseEntity.ok(customers);

    }

    @RequestMapping("get_all_customers")
    public String getAll(@RequestBody PageInfo pageInfo){
        try {
            PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
            //可以根据数据库设置一个state字段值，匹配在住的客户查询
            List<Customer> list = customerService.get_all_customers();
            PageInfo<Customer> pageInfoReturn = new PageInfo<>(list);
            return jsonReturn.returnSuccess(pageInfoReturn);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("get_all_customers_bed")
    public String getAllCustomer(@RequestBody PageInfo pageInfo){
        try {
            PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
            //可以根据数据库设置一个state字段值，匹配在住的客户查询
            List<Customer> list = customerService.get_all();
            PageInfo<Customer> pageInfoReturn = new PageInfo<>(list);
            return jsonReturn.returnSuccess(pageInfoReturn);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("get_all_customername")
    public String getAllName(){
        try {
            List<Customer> list = customerService.get_all_name();
            return jsonReturn.returnSuccess(list);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("get_iner_customernames")
    public String getInerNames(){
        try {
            List<Customer> list = customerService.get_iner_names();
            return jsonReturn.returnSuccess(list);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }


    @RequestMapping("get_customers_by")
    public String getByCondition(@RequestBody Customer customer){
        try {
            LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(!ObjectUtils.isEmpty(customer.getId()), Customer::getId, customer.getId())
                    .eq(!ObjectUtils.isEmpty(customer.getCustomerAge()), Customer::getCustomerAge, customer.getCustomerAge())
                    .eq(!ObjectUtils.isEmpty(customer.getCustomerSex()), Customer::getCustomerSex, customer.getCustomerSex())
                    .like(!ObjectUtils.isEmpty(customer.getCustomerName()), Customer::getCustomerName, customer.getCustomerName())
                    .eq(!ObjectUtils.isEmpty(customer.getBuildingNo()), Customer::getBuildingNo, customer.getBuildingNo());
            List<Customer> customers = customerService.list(queryWrapper);
            return jsonReturn.returnSuccess(customers);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }


    @RequestMapping("add_one_customer")
    public String addOne(@RequestBody Customer customer){
        try {
            customerService.addOne(customer);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("getcustomerby_id")
    public String getById(@RequestParam("customerid") Integer customerid){
        try {
            Customer customer = customerService.getById(customerid);
            return jsonReturn.returnSuccess(customer);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("modifycustomerby_id")
    public String modifyById(@RequestBody Customer customer){
        try {
            customerService.updateCustomer(customer);
            return jsonReturn.returnSuccess(customer);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("removecustomer_by_id")
    public String removeByid(@RequestParam("customerid") Integer customerid){
        try {
            customerService.removeOne(customerid);
            return jsonReturn.returnSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }




}
